<?php
/**
 * Created by Adon.
 * User: Adon
 * Date: 2017/8/18
 * Time: 14:17
 */

namespace app\modules\mch\controllers;
use yii\web\UploadedFile;
use app\models\Video;
use app\modules\mch\models\Model;
use yii\data\Pagination;
class VideoController extends Controller
{
    //视频列表
   public function actionIndex($cat_id = 1,$page=1)
   {

       $quest = Video::find()->where([
           'is_delete' => 1,
       ]);
       $list=$quest->orderBy('addtime DESC')->all();
       $count = $quest->count();
       $pagination = new Pagination(['totalCount' => $count, 'page' =>$page-1]);


       return $this->render('index', ['list'=>$list,'cat_id' => $cat_id,'row_count'=>$count,'pagination'=>$pagination]);
   }

   //视频修改/编辑
   public function actionEdit($cat_id,$id=null){

       $model = Video::findOne([
           'id' => $id,
       ]);


       if (\Yii::$app->request->isPost){
           $title=\Yii::$app->request->post('title');
           $content=\Yii::$app->request->post('content');
           $url=\Yii::$app->request->post('url');
           $sort=\Yii::$app->request->post('sort');

           if($cat_id == 2){
               $up=\Yii::$app->db->createCommand()->update('cshopmall_video',['title' => $title,'content'=>$content,'url'=>$url,'sort'=>$sort], "id = {$id}")->execute();
               if($up>0){
                   return [
                       'code' => 0,
                       'msg' => '保存成功',
                       'data'=>['cat_id'=>$cat_id]
                   ];
               }
           }else{
               $add= \Yii::$app->db->createCommand('INSERT INTO `cshopmall_video` (`title`,`content`,`url`,`addtime`,`sort`,`is_delete`) VALUES (:title,:content,:url,:addtime,:sort,:is_delete)', [
                   ':title' =>$title,
                   ':content'=>$content,
                   ':url'=>$url,
                   ':addtime'=>time(),
                   ':sort'=>$sort,
                   ':is_delete'=>1,
               ]  )->execute();
               if($add>0){
                   return [
                       'code' => 0,
                       'msg' => '保存成功',
                       'data'=>['cat_id'=>$cat_id]
                   ];
               }
           }



       }

       return $this->render('edit', ['model'=>$model]);

}


//删除视频

   public function actionDel($id){
       $model = Video::findOne([
           'id' => $id,
       ]);
       if ($model) {
           if($model['is_delete'] = 1){
               $update=\Yii::$app->db->createCommand()->update('cshopmall_video', ['is_delete' => 2], "id = {$id}")->execute();
               if($update>0){
                   return [
                       'code' => 0,
                       'msg' => '删除成功',
                   ];
               }
           }

       }
   }

}